#include "cxmainwindow.h"
#include <QtGui>

CXMainWindow::CXMainWindow(QWidget *parent):
_actionNewProject(0),
_actionOpenProject(0),
_actionSaveProject(0),
_actionSaveAsProject(0),
_projectToolBar(0)
{
    _readSettings();

    setCentralWidget(new QPlainTextEdit);

    _createActions();
    _createMenus();
    _createToolBars();
    _createStatusBar();

    setUnifiedTitleAndToolBarOnMac(true);
}

CXMainWindow::~CXMainWindow()
{
    _writeSettings();
}

void CXMainWindow::_slotNewProject()
{
    statusBar()->showMessage(tr("New Project"));
}

void CXMainWindow::_slotOpenProject()
{
    statusBar()->showMessage(tr("Open Project"));
}

void CXMainWindow::_slotSaveProject()
{
    statusBar()->showMessage(tr("Save Project"));
}

void CXMainWindow::_slotSaveAsProject()
{
    statusBar()->showMessage(tr("Save as Project"));
}

void CXMainWindow::_createActions()
{
    _actionNewProject = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
    _actionNewProject->setShortcuts(QKeySequence::New);
    _actionNewProject->setStatusTip(tr("Create a new project"));
    connect(_actionNewProject, SIGNAL(triggered()), this, SLOT(_slotNewProject()));

    _actionOpenProject = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
    _actionOpenProject->setShortcuts(QKeySequence::Open);
    _actionOpenProject->setStatusTip(tr("Open an existing project"));
    connect(_actionOpenProject, SIGNAL(triggered()), this, SLOT(_slotOpenProject()));

    _actionSaveProject = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
    _actionSaveProject->setShortcuts(QKeySequence::Save);
    _actionSaveProject->setStatusTip(tr("Save the project to disk"));
    connect(_actionSaveProject, SIGNAL(triggered()), this, SLOT(_slotSaveProject()));

    _actionSaveAsProject = new QAction(tr("Save &As..."), this);
    _actionSaveAsProject->setShortcuts(QKeySequence::SaveAs);
    _actionSaveAsProject->setStatusTip(tr("Save the project under a new name"));
    connect(_actionSaveAsProject, SIGNAL(triggered()), this, SLOT(_slotSaveAsProject()));

}

void CXMainWindow::_createMenus()
{
    _projectMenu = menuBar()->addMenu(tr("&Project"));
    _projectMenu->addAction(_actionNewProject);
    _projectMenu->addAction(_actionOpenProject);
    _projectMenu->addAction(_actionSaveProject);
    _projectMenu->addAction(_actionSaveAsProject);
}

void CXMainWindow::_createToolBars()
{
    _projectToolBar = addToolBar(tr("Project"));
    _projectToolBar->addAction(_actionNewProject);
    _projectToolBar->addAction(_actionOpenProject);
    _projectToolBar->addAction(_actionSaveProject);
}

void CXMainWindow::_createStatusBar()
{
    statusBar()->showMessage(tr("Ready"));
}

void CXMainWindow::_readSettings()
{
    QSettings settings("Trolltech", "Application Example");
    QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
    QSize size = settings.value("size", QSize(400, 400)).toSize();
    resize(size);
    move(pos);
}

void CXMainWindow::_writeSettings()
{
    QSettings settings("Trolltech", "Application Example");
    settings.setValue("pos", pos());
    settings.setValue("size", size());
}
